<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>移动零的位置</title>
</head>

<body>
  <script>
    //直接交换
    function arr(nums) {
      if (nums.length < 2) {
        return nums
      }
      let tmp = 0 //tmp指向0存在的位置
      for (let i = 0; i < nums.length; i++) {
        if (nums[i] != 0) {
          if (nums[tmp] == 0) {
            nums[tmp] = nums[i]
            nums[i] = 0
          }
          tmp++
        }
      }
      return nums
    };
    console.log(arr([0, 1, 0, 3, 12]));
  </script>
</body>

</html>